package com.asialjim.wechat.official.message;

import com.asialjim.wechat.apiservice.constant.WeChatConstants;
import com.asialjim.wechat.message.BaseWeChatMessage;
import com.asialjim.wechat.message.WeChatMessageType;
import com.asialjim.wechat.official.message.event.location.WeChatLocationEvent;
import com.asialjim.wechat.official.message.event.location.WeChatLocationSelectEvent;
import com.asialjim.wechat.official.message.input.*;
import com.asialjim.wechat.official.message.event.*;
import com.asialjim.wechat.pojo.account.WeChatAccountType;
import jdk.nashorn.internal.objects.NativeDebug;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;

/**
 * <h1><em>ASIAL JIM JAVA DOC</em></h1><hr/>
 * <h2>CLASS DESCRIPTION <i>[ NAME: WeChatOfficialMessageType ]</i></h2><strong>
 * <p> we-chat official message type
 * </strong><p><p><em>Copyright &copy; Asial Jim Co., LTD</em><hr/>
 *
 * @author Asial Jim &nbsp;&nbsp; <span>Email:<a href="mailto:asialjim@hotmail.com">asialjim@hotmail.com</a> &nbsp;&nbsp; <a href="asialjim@qq.com">asialjim@qq.com</a></span>
 * @version 1.0.0
 * @since 2021/3/8   &nbsp;&nbsp; JDK 8
 */
@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public enum WeChatOfficialMessageType implements WeChatMessageType {
    /**
     * 微信公众号文本消息
     */
    TEXT(WeChatAccountType.OFFICIAL_ACCOUNT, WeChatConstants.InputXmlMsgType.TEXT,WeChatConstants.StringConstants.EMPTY_STRING, WeChatInputTextMessage.class),
    /**
     * 微信图片消息
     */
    IMAGE(WeChatAccountType.OFFICIAL_ACCOUNT,WeChatConstants.InputXmlMsgType.IMAGE,WeChatConstants.StringConstants.EMPTY_STRING, WeChatInputImageMessage.class),
    /**
     * 微信语音消息
     */
    VOICE(WeChatAccountType.OFFICIAL_ACCOUNT,WeChatConstants.InputXmlMsgType.VOICE,WeChatConstants.StringConstants.EMPTY_STRING, WeChatInputVoiceMessage.class),
    /**
     * 微信视频消息
     */
    VIDEO(WeChatAccountType.OFFICIAL_ACCOUNT,WeChatConstants.InputXmlMsgType.VIDEO,WeChatConstants.StringConstants.EMPTY_STRING, WeChatInputVideoMessage.class),
    /**
     * 微信短视频消息
     */
    SHORT_VIDEO(WeChatAccountType.OFFICIAL_ACCOUNT,WeChatConstants.InputXmlMsgType.SHORTVIDEO,WeChatConstants.StringConstants.EMPTY_STRING,WeChatInputVideoMessage.class),
    /**
     * 微信链接消息
     */
    LINK(WeChatAccountType.OFFICIAL_ACCOUNT,WeChatConstants.InputXmlMsgType.LINK,WeChatConstants.StringConstants.EMPTY_STRING, WeChatInputLinkMessage.class),
    /**
     * 微信地址消息
     */
    LOCATION(WeChatAccountType.OFFICIAL_ACCOUNT,WeChatConstants.InputXmlMsgType.LOCATION,WeChatConstants.StringConstants.EMPTY_STRING,WeChatInputLocationMessage.class),
    /**
     * 点击菜单事件
     */
    CLICK_EVENT(WeChatAccountType.OFFICIAL_ACCOUNT,WeChatConstants.InputXmlMsgType.EVENT,WeChatConstants.InputXmlEventType.CLICK, WeChatClickEvent.class),
    /**
     * 上报地理位置事件
     */
    LOCATION_EVENT(WeChatAccountType.OFFICIAL_ACCOUNT,WeChatConstants.InputXmlMsgType.EVENT,WeChatConstants.InputXmlEventType.LOCATION, WeChatLocationEvent.class),
    /**
     * 扫码事件
     */
    SCAN_EVENT(WeChatAccountType.OFFICIAL_ACCOUNT,WeChatConstants.InputXmlMsgType.EVENT,WeChatConstants.InputXmlEventType.SCAN, WeChatScanEvent.class),
    /**
     * 关注事件
     */
    SUBSCRIBE_EVENT(WeChatAccountType.OFFICIAL_ACCOUNT,WeChatConstants.InputXmlMsgType.EVENT,WeChatConstants.InputXmlEventType.SUBSCRIBE, WeChatSubscribeEvent.class),
    /**
     * 取关事件
     */
    UNSUBSCRIBE_EVENT(WeChatAccountType.OFFICIAL_ACCOUNT,WeChatConstants.InputXmlMsgType.EVENT,WeChatConstants.InputXmlEventType.UNSUBSCRIBE,WeChatSubscribeEvent.class),
    /**
     * 点击菜单跳转链接事件
     */
    VIEW_EVENT(WeChatAccountType.OFFICIAL_ACCOUNT,WeChatConstants.InputXmlMsgType.EVENT,WeChatConstants.InputXmlEventType.VIEW, WeChatViewEvent.class),

    /**
     * template send job finish event
     */
    TEMPLATE_SEND_JOB_FINISH_EVENT(WeChatAccountType.OFFICIAL_ACCOUNT,WeChatConstants.InputXmlMsgType.EVENT, WeChatConstants.InputXmlEventType.TEMPLATE_SEND_JOB_FINISH,WeChatTemplateSendJobFinishEvent.class),

    VIEW_MINI_PROGRAM_EVENT(WeChatAccountType.OFFICIAL_ACCOUNT, WeChatConstants.InputXmlMsgType.EVENT, WeChatConstants.InputXmlEventType.VIEW_MINI_PROGRAM,WeChatViewMiniProgramEvent.class),

    LOCATION_SELECT_EVENT(WeChatAccountType.OFFICIAL_ACCOUNT,WeChatConstants.InputXmlMsgType.EVENT,WeChatConstants.InputXmlEventType.LOCATION_SELECT, WeChatLocationSelectEvent.class),

    /**
     * 默认消息类型,用户支持腾讯推送到本地，没有适配指定的消息/事件处理器的情况
     */
    DEFAULT(WeChatAccountType.OFFICIAL_ACCOUNT,WeChatConstants.InputXmlMsgType.TEXT,null,null),

    ;

    /**
     * 微信公众平台账户类型
     */
    private final WeChatAccountType accountType;

    /**
     * 消息类型
     */
    private final String msgType;

    /**
     * 事件类型
     */
    private final String eventType;

    /**
     * 包装类型
     */
    private final Class<? extends BaseWeChatMessage> messageClass;

    /**
     * <h3>METHOD DESCRIPTION <i>[ NAME: typeOf ]</i></h3>
     * get we-chat official message type
     * @param accountType {@link WeChatAccountType account type}
     * @param msgType {@link String message type}
     * @param eventType {@link String event type}
     * @return {@link WeChatOfficialMessageType message type}
     * @author Asial Jim &nbsp;&nbsp; <span>Email: &nbsp;&nbsp; <a href="mailto:asialjim@hotmail.com">asialjim@hotmail.com</a> &nbsp;&nbsp; <a href="asialjim@qq.com">asialjim@qq.com</a></span>
     * @since 2021/3/8 16:39
     */
    public static WeChatOfficialMessageType typeOf(final WeChatAccountType accountType, final String msgType, final String eventType){
        if (accountType == null || msgType == null || msgType.length() == 0){ return DEFAULT; }

        final String event = StringUtils.isEmpty(eventType) ? "" : eventType;

        return Arrays.stream(values())
                .filter(value -> value.accountType.equals(accountType)
                              && value.msgType.equals(msgType)
                              && value.eventType.equals(event))
                .findFirst()
                .orElse(DEFAULT);
    }
}